package com.stockz.ui.swing.charting.display.indicator;

import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.stockz.core.model.Bar;
import com.stockz.core.util.BarUtil;
import com.stockz.core.util.ListUtil;
import com.stockz.ui.swing.charting.model.Line;
import com.stockz.ui.swing.charting.model.Point;
import com.stockz.ui.swing.charting.painter.indicator.SupportResistenceIndicatorPainter;
import com.stockz.ui.swing.charting.util.UIChartingConstant.OVERLAY;

@Component
@Scope("prototype")
public class SupportResistence extends
		AbstractDisplayableIndicatorConfiguration {
	public static class Configuration{
		private double percentage = 20;

		public double getPercentage() {
			return percentage;
		}

		public void setPercentage(double percentage) {
			this.percentage = percentage;
		}
	}
	
	private Configuration configuration = new Configuration();
	private List<Line> lineList = new ArrayList<Line>();
	
	@Override
	public double[] getValues() {
		return new double[0];
	}
	
	public List<Line> getLines(){
		return lineList;
	}
	
	@Override
	public void paint(Graphics2D g) {
		SupportResistenceIndicatorPainter.paint(g, this);
	}
	
	@Override
	public void refreshValues() {
		List<Bar> barList = getStockChartPanel().getStockChartPanelState()
		.getBarList();
		
		List<Bar> extreams = ListUtil.getExtreams(barList, 
				configuration.getPercentage(), 
				getBarValueType());
		
		getStockChartPanel().getStockChartPanelState().getShapeSet().removeAll(lineList);
		lineList.clear();
		for(Bar bar : extreams){
			lineList.add(new Line(getStockChartPanel(),
					new Point(getStockChartPanel(),BarUtil.getValue(bar,getBarValueType()),barList.indexOf(bar)),
				 	new Point(getStockChartPanel(),BarUtil.getValue(bar,getBarValueType()),Integer.MAX_VALUE)));
		}
	}

	@Override
	public String getDescription() {
		return getShortName()+", "+configuration.getPercentage();
	}

	@Override
	public String getFullName() {
		return "Support/Resistence";
	}

	@Override
	public OVERLAY getOverlay() {
		return OVERLAY.PRICE;
	}

	@Override
	public String getShortName() {
		return "S&R";
	}

	@Override
	public boolean isOverlappable() {
		return true;
	}

	@Override
	public Object getConfiguration() {
		return configuration;
	}
}
